import { getme } from '@/api/user';
import options from './options'

export default options({
  state: {
    user: null
  },
  mutations: {
    setUser(state, user) {
      state.user = user
    },
    setToken(state, token) {
      localStorage.setItem('TOKEN', token)
    },
    outLogin(state) {
      localStorage.removeItem('TOKEN')
      state.user = null
    }
  },
  actions: {
    async sendMe({ commit }) {
      const data = await getme()
      if(data.code !== 200) return
      commit('setUser', data.user)
      return data.user
    }
  },
  getters: {
    portrait(state) {
      return '/image/' + (state.user.portrait || 'default.jpg')
    }
  },
})